package view;

import data.Data_Canvas;
import h2d.Tile;
import cmd.Cmd_ChangePage;
import cmd.Root_Cmd;
import cmd.Cmd_AddPage;
import tree.Root;
import data.Data_Book;
import tree.LeafKind;
import tree.LeafMsg;
import h2d.domkit.Object;
import h2d.Flow;


@:uiComp("view-timeline")
class ViewTimelineComp extends Flow implements Object {
    static var SRC = 
    <view-timeline>
    </view-timeline>

    public function new(?parent) {
		super(parent);
		initComponent();
	}
}

@:uiComp("pagebtn")
class PagebtnComp extends Flow implements Object {
    static var SRC = 
    <pagebtn>
        <flow class="pb" public id = "btn"></flow>
    </pagebtn>
    
    public function new(?p) {
        super(p);
        initComponent();
        btn.enableInteractive = true;
        
        btn.interactive.onClick = function(e) {
            OnClick();
        };
    }

    public dynamic function OnClick() {
        
    }
}

//工具的辅助界面
class View_Timeline extends ViewTress<ViewTimelineComp> {
    public function new(?view : ViewTimelineComp) {
        super(ViewTimelineComp, view);
    }

    public override function OnEnter(last:LeafKind) {
        super.OnEnter(last);
        
    }

    public override function OnUpdate(msg:LeafMsg) {
        super.OnUpdate(msg);
        if(msg != null){
            switch msg {
                case RefreshTimeline: RefreshView();
                default:
            }
        }
    }

    public function RefreshView() {
        var cnt = data_book.pageCount + 1;
        //m_view的第一个子物体是个默认物体
        for (i in 0 ... cnt){
            var pagebtn : PagebtnComp = cast m_view.getChildAt(i + 1);
            var id = i;
            if(pagebtn == null){
                pagebtn = new PagebtnComp(m_view);
                pagebtn.dom.applyStyle(m_style, true);
                pagebtn.OnClick = ()->{
                    if(data_canvas.isAutoPlay){
                        return;
                    }
                    //新增
                    if(id == data_book.pageCount){
                        var cmd = new Cmd_AddPage();
                        root_cmd.AddCmd(cmd);
                    }
                    //切换
                    else {
                        var cmd = new Cmd_ChangePage(id);
                        root_cmd.AddCmd(cmd);
                    }
                };
            }

            var frameColor = id == data_book.curpage ? 0xff0000 : 0x000000;
            pagebtn.backgroundTile = Tile.fromColor(frameColor);
            var width = data_canvas.width;
            var height = data_canvas.height;
            if(id < data_book.pageCount){
                var pixels = data_book.GetPagePixels(id);
                pagebtn.btn.backgroundTile = Tile.fromPixels(pixels);
            }
            else {
                pagebtn.btn.backgroundTile = Tile.fromColor(0xffff00);
            }
        }

        while (m_view.numChildren != cnt + 1){
            m_view.getChildAt(m_view.numChildren - 1).remove();
        }
        
    }
}